package com.fingard.dsp.bank.directbank.vbao03;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.detail.RetAtsBatPayRecDetail;
import com.fingard.text.StringHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class VBAO03BatPayRec extends VBAO03Base implements IBatPayRec {
    @Override
    public RetAtsBatPayRec d_1988_BatPay(ReqBatPayRec p_req1988) throws Exception {
        return d_BatPayRec(p_req1988);
    }

    @Override
    public RetAtsQryBatPayRec d_1998_QryBatPay(ReqQryBatPayRec p_req1998) throws Exception {
        return d_QryBatPayRec(p_req1998);
    }

    @Override
    public RetAtsBatPayRec d_9188_BatPay(ReqBatPayRec p_req9188) throws Exception {
        return d_BatPayRec(p_req9188);
    }

    @Override
    public RetAtsQryBatPayRec d_9198_QryBatPay(ReqQryBatPayRec p_req9198) throws Exception {
        return d_QryBatPayRec(p_req9198);
    }

    public RetAtsBatPayRec d_BatPayRec(ReqBatPayRec p_reqBat) throws Exception {
        RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_reqBat.reqHeader);
        retAts.reqSeqID = p_reqBat.reqBatchNO;

        try {
            ActSetItem actItem = getBankConfig().getActSetItem(p_reqBat.ownAct, "");
            if (actItem == null) {
                retAts.respCode = "0";
                retAts.respInfo = "未找到账号[" + p_reqBat.ownAct + "]商户配置信息!";
                retAts.transState = PaymentState.FAIL_Batch;
                retAts.payInfoCode = FailCode.E9981;

                retAts.writeRetToATS();
                return retAts;
            }

            Map<String,Object> listObject = new HashMap<String, Object>();

            List<Map> list = new ArrayList<Map>();
            String bankCode = null;
            while (p_reqBat.readRecord()) {
                HashMap<String, Object> singleDetail = new HashMap<String, Object>();
                singleDetail.put("itemNo", p_reqBat.readedDetail.askNumber);
                singleDetail.put("policyNo", p_reqBat.readedDetail.insuPolicyNum);
                bankCode = getBankConfig().getMapValueByKey("BankCodeMapping", p_reqBat.readedDetail.getOppBankCode());
                if (StringHelper.isNullOrEmpty(bankCode)) {
                    retAts.respCode = "0";
                    retAts.respInfo = "未找到BankCodeMapping中对应" + p_reqBat.readedDetail.getOppBankCode() + "配置信息,请检查config.xml相关配置!";
                    retAts.writeRetToATS();
                    return retAts;
                }
                singleDetail.put("bankCode", bankCode);
                singleDetail.put("cardName", p_reqBat.readedDetail.payName);
                singleDetail.put("cardNo", p_reqBat.readedDetail.payAct);
                if ("1".equals(p_reqBat.readedDetail.isPrivate)){
                    singleDetail.put("accountType", "private");
                } else {
                    singleDetail.put("accountType", "public");
                }
                if ("1".equals(p_reqBat.readedDetail.cardType)) {
                    singleDetail.put("cardType", "book");
                } else if ("2".equals(p_reqBat.readedDetail.cardType)) {
                    singleDetail.put("cardType", "debit");
                } else if ("3".equals(p_reqBat.readedDetail.cardType)) {
                    singleDetail.put("cardType", "credit");
                }
                singleDetail.put("mobile", p_reqBat.readedDetail.cellPhone);
                singleDetail.put("certNo", p_reqBat.readedDetail.certNum);
                singleDetail.put("certType", convertCertTypeToBank(p_reqBat.readedDetail.certType));
                singleDetail.put("amount", Long.parseLong(StringHelper.amountMultiply100(p_reqBat.readedDetail.payAmount)));
                singleDetail.put("currency", "CNY");
                singleDetail.put("subject", p_reqBat.readedDetail.purpose);
                singleDetail.put("remarks", p_reqBat.readedDetail.memo);
                list.add(singleDetail);
            }

            listObject.put("orderNo", p_reqBat.reqBatchNO);
            if (StringHelper.isNullOrEmpty(bankCode)) {
                retAts.respCode = "0";
                retAts.respInfo = "未找到BankCodeMapping中对应" + p_reqBat.readedDetail.payBankCode + "配置信息,请检查config.xml相关配置!";
                retAts.writeRetToATS();
                return retAts;
            }
            listObject.put("channelNo", bankCode);
            listObject.put("sumAmount", StringHelper.amountMultiply100(p_reqBat.totalAmt));
            listObject.put("sumNum", p_reqBat.totalNum);
            listObject.put("list", list);

            JSONObject jsonObjectReturn = sendToBank(JSONObject.toJSONString(listObject), actItem, bankFront.getUrl("batRecPay"));

            if (!"".equals(jsonObjectReturn) && jsonObjectReturn != null) {
                JSONObject respBizContent = decryptByRSA(jsonObjectReturn, actItem);
                String respCode = respBizContent.getString("returnCode");
                String tmpRespInfo = respBizContent.getString("returnMsg");
                if (respCode != null) {
                    retAts.respCode = "0";
                    retAts.respInfo = tmpRespInfo;
                    retAts.transState = respCode;
                    retAts.payInfoCode = respCode;
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpRespInfo;
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = "银行响应内容为空,请联系银行咨询!";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.toString();
            WriteBankLogLn(ex);
        }

        retAts.writeRetToATS();
        return retAts;
    }
    
    public RetAtsQryBatPayRec d_QryBatPayRec(ReqQryBatPayRec p_reqQryBat) throws Exception {
        RetAtsQryBatPayRec retAts = new RetAtsQryBatPayRec(p_reqQryBat.reqHeader);
        retAts.reqSeqID = p_reqQryBat.reqBatchNO;

        try {
            ActSetItem actItem = getBankConfig().getActSetItem(p_reqQryBat.ownAct, "");
            if (actItem == null) {
                retAts.respCode = "-1";
                retAts.respInfo = "未找到账号[" + p_reqQryBat.ownAct + "]商户配置信息";
                retAts.writeAfterDetail();
                return retAts;
            }
            Map<String,Object> map = new HashMap<String, Object>();
            map.put("orderNo", p_reqQryBat.reqBatchNO);

            JSONObject jsonObjectReturn = sendToBank(JSONObject.toJSONString(map), actItem, bankFront.getUrl("batRecPayQry"));

            if (!"".equals(jsonObjectReturn) && jsonObjectReturn != null) {
                JSONObject respBizContent = decryptByRSA(jsonObjectReturn, actItem);
                String respCode = respBizContent.getString("returnCode");
                String tmpRespInfo = respBizContent.getString("returnMsg");
                if (respCode != null) {
                    RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();
                    JSONArray tmpBatRespList = JSONArray.parseArray(respBizContent.getString("orderList"));
                    for(int i = 0; i < tmpBatRespList.size(); i++){
                        JSONObject tmpEachDetail = tmpBatRespList.getJSONObject(i);
                        tmpRetDetail.postScript = tmpEachDetail.getString("itemNo");
                        tmpRetDetail.payAmount = tmpEachDetail.getString("amount");
                        tmpRetDetail.result = respCode + "+" + tmpEachDetail.getString("orderStatus");
                        tmpRetDetail.payInfoCode = respCode;
                        tmpRetDetail.memo = tmpEachDetail.getString("errMsg");
                        retAts.writeRetDetail(tmpRetDetail);
                    }
                    retAts.respCode = "0";
                    retAts.respInfo = tmpRespInfo;
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpRespInfo;
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = "银行响应内容为空,请联系银行咨询!";
            }

        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }
        retAts.writeAfterDetail();
        return retAts;
    }
}
